﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Farigola.Engine.SystemData.Entity
{
    /// <summary>
    /// Represents an id for entities.
    /// 
    /// hide the implementation: Guid.
    /// </summary>
    public class EntityId
    {
        /// <summary>
        /// the concrete id implementation.
        /// </summary>
        private Guid _id = Guid.Empty;

        //---------------------------------------------------------------------
        /// <summary>
        /// Check the id, if not null, if set correctly.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool Check(EntityId id)
        {
            if (id == null)
                return false;

            return id.CheckInternal();
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Constructor, do nothing.
        /// </summary>
        public EntityId()
        {
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Constructor, generate an id now if true.
        /// </summary>
        /// <param name="generateIdNow"></param>
        public EntityId(bool generateIdNow)
        {
            if (generateIdNow)
                GenerateId();
        }

        //---------------------------------------------------------------------
        public bool CheckInternal()
        {
            if (_id == null)
                return false;

            return true;
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Generate a new id, only if not yet set.
        /// </summary>
        /// <returns></returns>
        public bool GenerateId()
        {
            if (_id != Guid.Empty)
                return false;

            _id = Guid.NewGuid();
            return true;
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Build a id from a string id format.
        /// return true if ok, false if the id is already set or the provided string id is wrong. 
        ///
        /// </summary>
        /// <param name="idStr"></param>
        /// <returns></returns>
        public bool SetFromIdStr(String idStr)
        {
            try
            {
                if (_id != Guid.Empty)
                    return false;

                _id = new Guid(idStr);
                return true;
            }
            catch (Exception e)
            {
                // error, the id is not set, equals null
                return false;
            }
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Return true if the provided id is equal to the inner id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IdAreEquals(EntityId id)
        {
            if (id == null)
                return false;

            if (id._id.Equals(this._id))
                return true;

            return false;
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// If the guid is not set (is set to 0)
        /// return '(null)'
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (_id == Guid.Empty)
                return "(null)";

            return _id.ToString();
        }
    }
}
